﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.Collections.Generic;
using HRProject.Code.DTO;

namespace HRProject.Department
{
    public partial class AddRoleToDepartment : System.Web.UI.Page
    {
        private Guid departmentId;
        private List<BLRole> roles;

        public List<BLRole> AvailableRoles
        {
            get { return roles; }
            set { roles = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string deptIdStr = Request.Params["departmentId"];
            try
            {
                departmentId = new Guid(deptIdStr);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                departmentId = Guid.Empty;
            }
            string departmentName = Request.Params["departmentName"];
            tbxDepartment.Text = departmentName;

            RoleDAO dao = new RoleDAO();
            BLDepartment filter = new BLDepartment();
            filter.Id = departmentId;
            roles = dao.getAvailableRolesToAdd(filter);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsAddAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            string[] addedRoles = Request.Params.GetValues("addedRole");
            for (int i = 0; i < addedRoles.Length; i++)
            {
                BLRole role = new BLRole();
                try
                {
                    role.Id = new Guid(addedRoles[i]);
                    role.Department = new BLDepartment();
                    role.Department.Id = departmentId;
                    role.DataSignature = 0;
                    RoleDAO dao = new RoleDAO();
                    int result = dao.insertRoleToDepartment(role);
                }
                catch (Exception exp)
                {
                    Console.Out.Write(exp.StackTrace);
                }
            }

            Util.CloseDialog(Response);
        }
    }
}
